package com.andatsoft.laisim.activity;

import java.io.File;

import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.db.DataBaseAccess;
import com.andatsoft.laisim.entity.Song;

public class SongDetailsActivity extends BaseActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_details);
		int songId = getIntent().getIntExtra(
				MainActivity.INTENT_DATA_MAIN_VIEW_DETAILS_SONG_ID, -1);

		if (songId == -1)
			return;
		DataBaseAccess db = DataBaseAccess.getInstance(getApplicationContext());
		Song song = db.getSong(songId);
		if (song == null)
			return;
		((TextView) findViewById(R.id.tv_main_view_detail_title)).setText(" "
				+ song.getTitle());

		((TextView) findViewById(R.id.tv_main_view_detail_address)).setText(" "
				+ song.getAddress());
		((TextView) findViewById(R.id.tv_main_view_detail_album)).setText(" "
				+ song.getAlbum());
		((TextView) findViewById(R.id.tv_main_view_detail_artist)).setText(" "
				+ song.getArtist());
		((TextView) findViewById(R.id.tv_main_view_detail_bit_rate))
				.setText(" " + song.getBitRateString());
		((TextView) findViewById(R.id.tv_main_view_detail_duration))
				.setText(" " + song.getDurationString());
		((TextView) findViewById(R.id.tv_main_view_detail_genre)).setText(" "
				+ song.getGenre());

		File f = new File(song.getAddress());
		if (f != null) {
			((TextView) findViewById(R.id.tv_main_view_detail_length))
					.setText(" " + f.length() / 1024 + " KB");
		}

		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		try {
			retriever.setDataSource(song.getAddress());
			String metaData = "";
			metaData = retriever
					.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
			if (metaData == null)
				metaData = getString(R.string.common_text_unknown);
			((TextView) findViewById(R.id.tv_main_view_detail_composer))
					.setText(" " + metaData);

			metaData = retriever
					.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
			if (metaData == null)
				metaData = getString(R.string.common_text_unknown);
			((TextView) findViewById(R.id.tv_main_view_detail_year))
					.setText(" " + metaData);

			metaData = retriever
					.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);
			if (metaData == null)
				metaData = getString(R.string.common_text_unknown);
			((TextView) findViewById(R.id.tv_main_view_detail_track))
					.setText(" " + metaData);

			metaData = retriever
					.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPILATION);
			if (metaData == null)
				metaData = getString(R.string.common_text_unknown);
			((TextView) findViewById(R.id.tv_main_view_detail_compilation))
					.setText(" " + metaData);

			metaData = retriever
					.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
			if (metaData == null)
				metaData = getString(R.string.common_text_unknown);
			((TextView) findViewById(R.id.tv_main_view_detail_format))
					.setText(" " + metaData);

			byte[] albumart = retriever.getEmbeddedPicture();
			if (albumart == null) {
				metaData = getString(R.string.view_detail_text_album_art_no);
			} else {
				metaData = getString(R.string.view_detail_text_album_art_yes);
			}
			((TextView) findViewById(R.id.tv_main_view_detail_album_art))
					.setText(" " + metaData);
			albumart = null;
		} catch (Exception e) {

		} finally {
			if (retriever != null)
				retriever.release();
		}

	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
	}
}
